package com.singzi.leetcode.jianzhiOffer2;

import com.singzi.leetcode.obj.TreeNode;

import java.util.*;

public class T0043 {
}
class CBTInserter {
    TreeNode root;
    List<TreeNode> list;
    public CBTInserter(TreeNode root) {
        this.root = root;
        list = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            TreeNode t = queue.remove();
            list.add(t);
            if(t.right != null) queue.add(t.right);
            if(t.left != null) queue.add(t.left);

        }

    }

    public int insert(int v) {
        int index = (list.size() + 1)/2;
        TreeNode t = new TreeNode(v);
        if(index % 2 == 0) list.get(index).left = t;
        else list.get(index).right = t;
        list.add(t);
        return list.get(index).val;

    }

    public TreeNode get_root() {

        return  root;
    }
}